package be.sbw.appengine.site.publisher.frontend;

import be.sbw.wave.extensions.publisher.model.PMF;
import be.sbw.wave.extensions.publisher.model.WaveDocument;
import be.sbw.wave.extensions.publisher.publishy.PublishyServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

@SuppressWarnings("serial")
public class XmlServlet extends HttpServlet {
	
	private static final Logger LOG = Logger.getLogger(PublishyServlet.class.getName());
	
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
    	resp.setContentType("text/xml");
    		// get parameter
    	String id=req.getParameter("id");

		WaveDocument wd = null;
		LOG.info("Searching for "+id);
    	if(id!=null){
    		PersistenceManager pm = PMF.get().getPersistenceManager();
    	    wd = WaveDocument.GetWave(pm, id);
    	    LOG.info("Wave document searched with id:"+id);
    	}else{
    		id="not defined";
    	}
		createXmlOutput(resp, wd, id);
    }

	private void createXmlOutput(HttpServletResponse resp, WaveDocument wd,String query)
			throws TransformerFactoryConfigurationError, IOException {
		String waveId="";
		String waveletId="";
		String blipId="";
		String title="";
		String authors="";
		String content="";
		String htmlContent="";
		if(wd!=null){
			waveId = wd.getWaveId();
			waveletId=wd.getWaveletId();
			blipId = wd.getBlipId();
			title = wd.getBlipTitle();
			authors = wd.getAuthors();
			content = wd.getBlipContent();
			htmlContent = wd.getBlipHtmlContent();
			LOG.info("WD is not null");
		}
		try {
			LOG.info("waveId:"+waveId);
			LOG.info("waveletId:"+waveletId);
			LOG.info("blipId:"+blipId);
			LOG.info("title:"+title);
			LOG.info("authors:"+authors);
			LOG.info("content:"+content);
			SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
			TransformerHandler hd;
			PrintWriter out = resp.getWriter();
			StreamResult streamResult = new StreamResult(out);
			hd = tf.newTransformerHandler();
			Transformer serializer = hd.getTransformer();
			serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
			serializer.setOutputProperty(OutputKeys.INDENT,"yes");
			serializer.setOutputProperty(OutputKeys.VERSION,"1.0");
			hd.setResult(streamResult);
			hd.startDocument();
			AttributesImpl atts = new AttributesImpl();
			atts.addAttribute("", "", "request", "CDATA", query);
			hd.startElement("", "", "Query", atts);
			atts.clear();
			atts.addAttribute("","", "waveid", "CDATA", waveId);
			hd.startElement("", "", "Wave", atts);
			atts.clear();
			hd.startElement("", "", "Title", atts);
			hd.characters(title.toCharArray(),0,title.length());
			hd.endElement("", "", "Title");
			hd.startElement("", "", "Authors", atts);
			hd.characters(authors.toCharArray(),0,authors.length());
			hd.endElement("", "", "Authors");
			atts.addAttribute("","", "waveletid", "CDATA", waveletId);
			hd.startElement("","","Wavelet",atts);
			atts.clear();
			atts.addAttribute("","", "blipid", "CDATA", blipId);
			hd.startElement("","","Blip",atts);
			atts.clear();
			hd.startElement("", "", "Content", atts);
			hd.characters(content.toCharArray(),0,content.length());
			hd.endElement("","","Content");
			hd.startElement("", "", "HtmlContent", atts);
			hd.startCDATA();
			hd.characters(htmlContent.toCharArray(),0,htmlContent.length());
			hd.endCDATA();
			hd.endElement("","","HtmlContent");
			hd.endElement("","","Blip");
			hd.endElement("","","Wavelet");
			hd.endElement("","","Wave");
			hd.endElement("","","Query");
			hd.endDocument();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
